package org.sy.lab.多线程基础._4_执行器._1_创建线程池._2_newCachedThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import org.sy.lab.多线程基础._4_执行器._1_创建线程池._1_newFixedThreadPool.MyThread;

//创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待
public class Main {
	
	public static void main(String[] args) {
		
		ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(3);
		
		for (int i = 0; i < 5; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		
		for (int i = 5; i < 10; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		
		for (int i = 10; i < 15; i++) {
			MyThread myThread = new MyThread("任务" + (i+1));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			executor.execute(myThread);
		}
		executor.shutdown();
	}
}
